<?
include("session.php"); 

$id = "";
if(isset($_GET['id'])) {
	$id = $_GET['id'];
}

$has_done_saved = "false";
if(isset($_GET['has_done_saved'])) {
	$has_done_saved = $_GET['has_done_saved'];
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../../../webkit/utils/jquery.js"></script>
<script type="text/javascript" src="../../../webkit/utils/form.checker.js"></script>
<script type="text/javascript" src="../../../webkit/datepicker/WdatePicker.js"></script>
<link type="text/css" rel="stylesheet" href="../../page.css"/> 
<body  bgcolor="#f4f4f4" style="margin:0px;" scroll=no>
<style> 
	.tab {
		font-size:12px; background:#ccc;  border:gray 1px solid; cursor:pointer; margin-right:6px; padding:2px 5px;
	}
	.tab_current {
		background:#b0d2fd;  border-bottom-color:#6600ff;
	}
	.input {width: 300px}
</style>

<!-- Tab Begin -->
<span class='tab' id='basic_info' onclick='show_tab(this.id)'>客户信息</span> 
<span class='tab' id='text_info' onclick='show_tab(this.id)'>营销模板</span>
<!-- Tab End -->

<hr size=1>

<table width="100%" cellspacing=1 cellpadding=3 id="basic_info_table"> 
<form name=f> 
	<tr>
		<td align=right nowrap>客户姓名</td><td><input type='text' id="cust_name" class='input' check='not_empty' warning='客户姓名不能为空！' style="color:blue"></td>  
		<td align=right>彩铃功能</td><td><select id="ring_flag" class='input'><option value=0></option><option value=1>是</option><option value=2>否</option></select></td>
	</tr>
	<tr>
		<td align=right>客户电话</td><td><input type='text' id="telephone" class='input' check='not_empty' warning='联系电话不能为空！' style="color:red" style="width:200px">
		<span id="call_btn"></span></td> 
		<td align=right>客户区域</td><td><input type='text' id="cust_addr" class='input' value='' style="width:120px">　联系人：<input type='text' id="cust_person" class='input' value='' style="width:120px"></td>
	</tr>
	<tr>
		<td align=right>客户备注</td><td colspan=3><textarea style="width:680px;" rows=2 id="cust_desc"></textarea></td>
	</tr>
	<tr>
		<td align=right nowrap>营销结果</td><td><select id="call_result" class='input'><option value='0'>-未呼-</option><option value='1'>营销成功</option><option value='2'>营销失败</option><option value='3'>预约回拔</option><option value='4'>集团彩铃</option><option value='5'>忙音</option><option value='6'>空号</option><option value='7'>停机</option><option value='8'>无人接听</option><option value='9'>直接挂机</option><option value='10'>非负责人</option><option value='11'>沟通障碍</option><option value='12'>无效数据</option></select></td>
		<td align=right nowrap>预约时间</td><td><input type="text" class='input' id="appt_time" onClick="WdatePicker({readOnly:true, skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" size="15"></td>
	</tr>
	<tr>

		<td align=right nowrap>二次营销</td><td>
		<input type="text" value="" id="recall_tel" class='input' onkeyup="if(event.keyCode==13){prepare()} else {$('#iframe_td').html('')}" style="width:180px">
		<input type="button" value="确认号码" onclick="prepare()" style="border:gray 1px solid" id="btn_prepare_call">
		<!--用于二次确认营销的号码-->
		<span id="recall_btn"></span><span id="recall_voice_td"></span></td>
		<td align=right>服务号码</td><td><input type='text' id="call_tel" class='input' style="color:#3300cc" readonly value=""></td>
	</tr>
	<tr>
		<td align=right>沟通内容</td><td colspan=3><textarea style="width:680px;" rows=3 id="call_content"></textarea></td>
	</tr>
	<tr>
		<td align=right>录音编号</td><td colspan=3>
		<input type='text' id="call_id" class='input' style="color:#cc00cc; width:220px" readonly value='<?echo $_GET['call_id']?>'>　
		二次：<input type='text' id="recall_id" class='input' style="color:#cc00cc; width:220px" readonly> <!--二次营销时附值-->
		</td>
	</tr>
	<input type='hidden' id="agent_id" value="<?echo $_GET['agent_id']?>">
	<input type='hidden' id="id" value="<?echo $_GET['id']?>">
	<input type='hidden' id="task_id" value="">
</form>	
</table>

<table width=100% cellspacing=1 cellpadding=1 id="text_info_table" style="display:none">
	<tr>
		<td width=70%>
			<iframe src="text/text_sample.html" width="100%" height="300" frameborder=0 name="text_file_frame"></iframe>
		</td>
	</tr>
</table>


<table width=100% cellspacing=1 cellpadding=3>
	<tr>
		<td align=center>
		<hr size=1>
		<!--<input type="button" id="btn_prev" class="button" value="上一条" onclick="to_near_record('prev')" style="color:#cc0066">　　--><input type="button" id="btn_save" class="button" value="保存信息" onclick="save()" disabled>　　<input type="button" id="btn_close" class="button" value="关闭窗口" onclick="close_dlg()" disabled>　　<input type="button" id="btn_next" class="button" value="下一条" onclick="to_near_record('next')" style="color:blue" disabled></td>
	</tr>
</table>

<script type="text/javascript">
	var f = document.f;
	var agent_id = "<?echo $_GET['agent_id']?>";

	function prepare() {
		var tel = $("#recall_tel").val();
		if(tel == "") {
			alert("请输入客户电话号码！");
			return; 
		}
		parent.is_recall = "yes";
		var str = "<iframe src='http://222.240.139.227:9080/agent_event/callout_button.jsp?caller="+$("#call_tel").val()+"&called="+$("#recall_tel").val()+"&iconSize=20&calloutParams=no_confirm_before_call' width=21 height=21 frameborder=0 marginheight=0 marginwidth=0 scrolling=no align=top></iframe>";
		$("#recall_btn").html(str);

		$("#btn_prepare_call").attr("disabled", true);
		$.get("call_query.php?action=check_has_called&tel="+tel+"&task_id="+$("#task_id").val(), function(data) {
			if(data == "yes") {
				alert('客户号码('+tel+')已经拔打过，请避免重复操作！');
			}
			$("#btn_prepare_call").attr("disabled", false);
		});
	}


	function close_dlg() {
		if(confirm('你尚未保存当前客户信息，确定要关闭吗？')) {
			parent.is_recall = "no"; // 重置二次营销外呼的标志
			if( has_done_saved ) {
				parent.refreshGrid(); // 关闭外呼窗口时，刷新一下列表；2014/10/29
			}
			parent.closeDlg();
		}
	}

	<?if(isset($_GET['go_to_method'])) {?>
	var go_to_method = "<?echo $_GET['go_to_method']?>";
	<?} else {?>
	var go_to_method = "near"; // 默认动作：取已分配的最新一条记录; 
	<?}?>

	function to_near_record(d) {
		var params = {'id':$('#id').val(), 'direction':d}
		var url = "call_query.php?action=get_near_id&agent_id="+agent_id+"&task_id="+$("#task_id").val();
		$('#btn_next').val('正在读取中...');
		$('#btn_next').attr('disabled', true);
		$.get(url, params, function(data) {
			//alert(data)
			if(data=="") {
				if(confirm("当前已分配的记录已经完成，是否自动提取任务中的可呼记录？")) {
					go_to_method = "free";
					to_free_record();
				} else {
					$('#btn_next').val('下一条');
					$('#btn_next').attr('disabled', false); // 放开next按键; 2014/11/29
					$('#btn_close').attr('disabled', false); // 放开close按键; 2014/11/10
				}
				return false;
			}
			window.location = "call_panel.php?op=edit&agent_id="+agent_id+"&id="+data+"&has_done_saved="+has_done_saved;
		});
	}

	function to_free_record() {
		var url = "call_query.php?action=get_free_id&agent_id="+agent_id+"&task_id="+$("#task_id").val();
		$.get(url, {}, function(data) {
			if(data=="") {
				alert("当前任务已经没有待分配的可呼记录了，请重新导入数据！");
				$('#btn_next').val('无可呼记录');
				$('#btn_close').attr('disabled', false); // 放开close按键; 2014/11/10
				return false;
			}
			window.location = "call_panel.php?op=edit&agent_id="+agent_id+"&id="+data+"&has_done_saved="+has_done_saved+"&go_to_method=free";
		});	
	}

    var has_done_saved = <?echo $has_done_saved?>; // 是否有做过保存操作; 2014/10/29

	function save() { 
		if(FormChecker.doCheck(f)) {
			var params = {};
			var els = f.elements;
			for(var i=0; i<els.length; i++) {
				if(els[i].type == "button") continue;
				params[els[i].id] = els[i].value;
			}
			if(parent.is_recall == "yes") { // 保存二次营销id
				params['recall_id'] = parent.recall_id; 
			}
			if(confirm('确定要保存客户信息吗？')) {
				$('#btn_save').attr('disabled', true);
				$('#btn_close').attr('disabled', true);
				$('#btn_next').attr('disabled', true);
				$('#btn_save').val('保存中..');
				$.post("call_query.php?action=<?echo $_GET['op']?>", params, function(data) {
					if(data.indexOf("ok") == 0) {
						alert("数据保存成功!");
						parent.is_recall = "no"; // 重置二次营销外呼的标志
						//parent.refreshGrid(); // 不再每次刷新列表，以节省系统消耗。改为在关闭窗口时refresh；2014/10/29
						//parent.closeDlg();
						has_done_saved = true;
						if(go_to_method == "near") {
							to_near_record('next'); // 不关闭窗口，改为直接跳至下一条; 2014/6/29
						} else {
							to_free_record(); // 已分配的记录完成后，开始直接自动提取；2014/11/29
						}
					} else {  
						alert(data);
					}
					//$('#btn_save').attr('disabled', false);
					$('#btn_save').val('保存成功');
					//$('#btn_close').attr('disabled', false);
					//$('#btn_next').attr('disabled', false);

				});
			}
		}
	}; 

	
	function get_record_by_tel(tel) {
		$.get("call_query.php?action=get_by_tel&tel="+tel, function(data){
			if(data == "no record") {
				alert('没有找到号码为['+tel+']的客户记录！');
				return false
			}
			eval("var row = "+data);
			for(var k in row) {
				$("#"+k).val(row[k]);
			}
			$('#call_id').val('<?echo $_GET['call_id']?>');  // 弹屏时传入的call_id
			$('#call_tel').val('<?echo $_GET['call_tel']?>');  // 弹屏时传入的call_tel
			$('#recordfile').val('<?echo $_GET['recordfile']?>');  // 弹屏时传入的call_tel
			if(row['text_file'] != '') {
				document.frames['text_file_frame'].location = 'text/'+row['text_file'];
			}
			init_recall_voice_link(row['recall_id']);
			init_call_button(row['telephone']);
		})
	}

	var task_is_paused = false; // 任务是否已被暂停；2014/12/11

	function get_record_by_id(id) {
		$.get("call_query.php?action=get_by_id&id="+id, function(data){
			eval("var row = "+data);
			if(row['task_status'] == 3) {
				alert("此任务已被暂停，不允许进行外呼操作！");
				//parent.closeDlg();
				task_is_paused = true;  // 不直接关掉窗口，用一个标志来控制；2014/12/11
			}
			for(var k in row) {
				$("#"+k).val(row[k]);
			}
			if(row['text_file'] != '') {
				document.frames['text_file_frame'].location = 'text/'+row['text_file'];
			}
			if(row['call_tel'] == "") {
				$("#call_tel").val(row['callout_number']); // 直接用任务配置里的外显号码赋值
			}
			if( ! task_is_paused ) { // 未暂停的任务，则显示外呼按钮及功能按钮; 
				init_recall_voice_link(row['recall_id'], row['recall_tel'])
				init_call_button(row['telephone']);
				//if($("#call_result").val()!=1 || agent_id=='hncsgj01') { // 营销成功的数据，不允许再修改(班长除外)；2014/12/10
				//	$('#btn_save').attr('disabled', false);
				//}
				$('#btn_save').attr('disabled', false);
				if($("#call_result").val() == 0) { // 只有未呼数据，才允许进行“下一条”操作; 2014/12/10
					$('#btn_next').attr('disabled', false);
				}
			}
			$('#btn_close').attr('disabled', false);
		})
	}

	function init_recall_voice_link(recall_id, telephone) { // 显示二次营销录音播放链接
		if(recall_id != "") {
			$("#recall_voice_td").html("<img src='voice2.jpg' width=16 height=16 onclick=\"show_voice('"+recall_id+"', '"+telephone+"')\"  align=absmiddle style=\"cursor:hand\" title=\"点击播放录音\">");
		}
	}

	function  init_call_button(tel) {		
		var str = "<iframe src='http://222.240.139.227:9080/agent_event/callout_button.jsp?caller="+$("#call_tel").val()+"&called="+tel+"&iconSize=20&calloutParams=no_confirm_before_call' width=21 height=21 frameborder=0 marginheight=0 marginwidth=0 scrolling=no align=top></iframe>";
		$("#call_btn").html(str);
	}

	function show_voice(rid, tel) {
		window.showModelessDialog("http://222.240.139.227/agent_event/api/play_voice.jsp?rid="+rid+"&tel="+tel,"dialogwin","location:0;scroll:0;status:0;resizable:0;dialogWidth:520px;dialogHeight:150px");
	}


	$(function() {
		<?if(isset($_GET['telephone'])) {?>
		get_record_by_tel('<?echo $_GET['telephone']?>'); 
		<?} else {?>
		get_record_by_id('<?echo $_GET['id']?>'); 
		<?}?>
	});


	$('#basic_info').addClass('tab_current');
	var current_tab_id = 'basic_info';
	function show_tab(id) {
		if(id != current_tab_id) {
			$('#'+current_tab_id).removeClass('tab_current');
			$('#'+current_tab_id+'_table').hide();
			$('#'+id).addClass('tab_current');
			$('#'+id+'_table').show();
			current_tab_id = id;
		}
	}
</script>
</body>
</html>